package com.example.demo.demos.web.controller;
import com.example.demo.demos.web.entity.User;

import com.example.demo.demos.web.mapper.UserMapper;
import com.example.demo.demos.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.demo.demos.web.controller.TokenUtil;
@RestController
@RequestMapping("index")
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private UserService userService;
//管理员登录
    @GetMapping("/user")
    public Map<String,Object> index(@RequestParam String username,@RequestParam String password){
       Map<String,Object> res=new HashMap<>();
        System.out.println(username);
        System.out.println(password);
       List<User> data=userMapper.selects(username,password);
//        System.out.println(data.get(0));
        String type="";
        String status="";
        if(!data.isEmpty()){
            for (User ress:data){
                System.out.println(ress.getId());
                type=ress.getType();
                status=ress.getStatus();
            }

        }
      System.out.println(type);
        System.out.println(status);
       System.out.println("321");
        System.out.println("token1");
        String token=TokenUtil.token(username,password);
        System.out.println(token);
        System.out.println("token2");
       if(data.isEmpty()){
           res.put("code",500);
           res.put("msg","该用户不存在");
       }else{
           System.out.println(type.getClass());
           System.out.println(type.equals("2"));
           if(type.equals("2")){
               if(status.equals("0")){
                   res.put("code",500);
                   res.put("msg","该用户在审核中，请审核通过后再登陆");

               }
               if(status.equals("1")){
                   res.put("code",500);
                   res.put("msg","该用户被封禁，请联系管理员");

               }

               if(status.equals("2")){


                   res.put("code",200);
                   res.put("data",data.get(0));
                   res.put("token",token);
                   res.put("msg","登录成功");

               }


           }else{
               res.put("code",200);
               res.put("data",data.get(0));
               res.put("token",token);
           }

       }

        return res;
    }
    @PostMapping("/adduser")
    public Map<String,Object> register(@RequestBody User user){
        Map<String,Object> res=new HashMap<>();

       if(user.getId()==null){

           Integer isgb=userService.adduserservice(user);
           if(isgb==1){
               res.put("code",200);
               res.put("msg","注册成功");
           }else{
               res.put("code",500);
               res.put("msg","注册失败");

           }

       }else{
           Integer isgb=userService.updateservice(user);
           if(isgb==1){
               res.put("code",200);
               res.put("msg","修改成功");
           }else{
               res.put("code",500);
               res.put("msg","修改失败");

           }

       }
        return res;
    }
    @GetMapping("/userlist")
    public Map<String,Object> userindex(@RequestParam String username, @RequestParam Integer pageNum,@RequestParam Integer pageSize){
        Map<String,Object> res=new HashMap<>();
        pageNum=(pageNum-1)*pageSize;
        System.out.println(username);

       List<User> data=userMapper.selectuser(username,pageNum,pageSize);
Integer total=userMapper.usertotal(username);
       res.put("data",data);
       res.put("total",total);
        return res;
    }

}
